package com.jsgt.greekfire.ui.trading.billcoin

import android.view.View
import android.widget.PopupWindow
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.ScreenUtil
import com.jsgt.greekfire.PAGE_SIZE
import com.jsgt.greekfire.R
import com.jsgt.greekfire.ui.trading.billcoin.adapter.BillAdapter
import com.jsgt.greekfire.factory.UserViewModelFactory
import com.jsgt.greekfire.viewmodel.UserViewModel
import kotlinx.android.synthetic.main.activity_bill_coin.*

/**
 * 币币账单
 * author fhx
 * create time 2020/4/24 9:57
 **/
class BillCoinActivity : BaseActivity(), View.OnClickListener {
    private val userViewModel: UserViewModel by lazy {
        ViewModelProvider(
            this,
            UserViewModelFactory()
        ).get(UserViewModel::class.java)
    }
    private var allTypeWindow: PopupWindow? = null
    override fun getLayoutId(): Int = R.layout.activity_bill_coin

    override fun main() {
        title = getString(R.string.coin_bill)
        val billAdapter =
            BillAdapter()
        bill_rv.adapter = billAdapter

        val coinTypeView = layoutInflater.inflate(R.layout.view_coin_type_window, null, false)
        val coinTypeWindow = PopupWindow(
            coinTypeView,
            ScreenUtil.getScreenWidth(this),
            ScreenUtil.dip2px(this, 245f).toInt()
        )
        coinTypeWindow.isOutsideTouchable = true
        val allTypeView = layoutInflater.inflate(R.layout.view_all_type_window, null, false)
        allTypeView.findViewById<TextView>(R.id.all_type_tv).setOnClickListener(this)
        allTypeView.findViewById<TextView>(R.id.buying_tv).setOnClickListener(this)
        allTypeView.findViewById<TextView>(R.id.sell_tv).setOnClickListener(this)
        allTypeView.findViewById<TextView>(R.id.recharge_tv).setOnClickListener(this)
        allTypeView.findViewById<TextView>(R.id.withdraw_tv).setOnClickListener(this)

        allTypeWindow = PopupWindow(
            allTypeView,
            ScreenUtil.getScreenWidth(this),
            ScreenUtil.dip2px(this, 245f).toInt()
        )
        allTypeWindow?.isOutsideTouchable = true
        coin_type_tv.setOnClickListener {
            if (coinTypeWindow.isShowing) {
                coinTypeWindow.dismiss()
            } else {
                coinTypeWindow.showAsDropDown(type_ll)
                allTypeWindow?.dismiss()
            }
        }

        all_type_tv.setOnClickListener {
            if (allTypeWindow?.isShowing!!) {
                allTypeWindow?.dismiss()
            } else {
                allTypeWindow?.showAsDropDown(type_ll)
                coinTypeWindow.dismiss()
            }
        }
        userViewModel.assetsLogList("2", "", 1, PAGE_SIZE)
        userViewModel.coinBillLiveData.observe(this, Observer {
            billAdapter.refreshData(it)
        })
    }

    override fun onClick(v: View?) {
        var assetEvent: String? = null
        when (v!!.id) {
            R.id.all_type_tv -> {
                all_type_tv.text = "全部类型"
                assetEvent = ""
            }
            R.id.recharge_tv -> {
                all_type_tv.text = "充币"
                assetEvent = "1"
            }
            R.id.withdraw_tv -> {
                all_type_tv.text = "提币"
                assetEvent = "2"
            }
            R.id.buying_tv -> {
                all_type_tv.text = "买入"
                assetEvent = "3"
            }
            R.id.sell_tv -> {
                all_type_tv.text = "卖出"
                assetEvent = "4"
            }
        }
        allTypeWindow?.dismiss()
        userViewModel.assetsLogList("2", assetEvent!!, 1, PAGE_SIZE)
    }
}